import sys
from PyQt5 import QtNetwork, QtCore, QtWidgets
from PyQt5.QtCore import Qt, QThread, pyqtSignal
from PyQt5.QtGui import QTextCursor


class PathChoice(QtWidgets.QWidget):
    """选择路径"""

    def __init__(self, parent=None):
        super().__init__(parent)

        self.file_or_folder_path = None

        self.protocol_combo = QtWidgets.QComboBox(self)
        self.protocol_combo.addItem("单文件")
        self.protocol_combo.addItem("多个文件")

        self.label = QtWidgets.QLabel("请选择路径!")
        button = QtWidgets.QPushButton("选择文件路径", self)
        button.clicked.connect(self.open_folder_file_dialog)

        self.label_path = QtWidgets.QLabel("未选择路径")

        layout1 = QtWidgets.QHBoxLayout()
        layout1.addWidget(self.protocol_combo)
        layout1.addWidget(button)

        layout2 = QtWidgets.QVBoxLayout()
        layout2.addWidget(self.label)
        layout2.addLayout(layout1)

        # 设置窗口的主要布局
        self.setLayout(layout2)

    def open_folder_file_dialog(self):
        # 打开路径对话框
        path = None
        print(self.label.text())
        if self.protocol_combo.currentText() == "单文件":
            path = QtWidgets.QFileDialog.getOpenFileNames(self, "选择文件夹或文件", "/", "All Files (*.*)")
            if path[0]:
                # self.append_text(self.label, path[0])
                self.label.setText(path)
        elif self.protocol_combo.currentText() == "多个文件":
            path = QtWidgets.QFileDialog.getExistingDirectory(window, "选择文件夹")
            # self.append_text(self.label, path)
            self.label.setText(path)
        print(self.label.text())
        self.file_or_folder_path = path

    @staticmethod
    def append_text(method: QtWidgets.QLabel, text):
        """
        给QLabel添加内容
        :param method:
        :param text:
        :return:
        """
        current_text = method.text()
        new_text = current_text + text
        method.setText(new_text)


class MainWindow(QtWidgets.QWidget):

    def __init__(self):
        super().__init__()

        self.path_choice = PathChoice(self)

        main_layout = QtWidgets.QVBoxLayout()
        main_layout.addWidget(self.path_choice)
        self.setLayout(self.path_choice)


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    window = PathChoice()
    window.show()
    sys.exit(app.exec_())
